<template>
  <el-container>
    <el-header class="header-login">
      <img src="@/assets/logo.png" alt="404">
    </el-header>
    <el-main class="content">
      <el-aside width="344px" class="login">
        <el-row>
          <span style="font-size: 22px;color: #000">常德人民检察院工资查询系统</span>
        </el-row>
        <br/>
        <el-row>
          <span style="font-size: 20px;color: #000">账号登陆</span>
        </el-row>
        <el-row style="margin-top: 20px" :gutter="20">
          <el-col>
            <el-input
              v-model="loginForm.username"
              placeholder="请输入账号"
              prefix-icon="el-icon-user-solid"
            ></el-input>
          </el-col>
        </el-row>
        <el-row style="margin-top: 20px" :gutter="20">
          <el-col>
            <el-input
              v-model="loginForm.password"
              placeholder="请输入密码"
              prefix-icon="el-icon-lock"
              show-password
            ></el-input>
          </el-col>
        </el-row>
        <el-row style="margin-top: 20px;text-align: left;margin-left: 2px;" :gutter="20">

<!--          <el-checkbox v-model="checked">记住密码</el-checkbox>-->
          <el-button type="text" style="margin-left: 150px;color: #409EFF;display: none">忘记密码？</el-button>
        </el-row>

        <el-row style="margin-top: 20px" :gutter="20">
          <el-col>
            <el-button :loading="loading" type="primary" style="width: 300px" @click.native.prevent="handleLogin">登陆
            </el-button>
          </el-col>
        </el-row>
      </el-aside>
    </el-main>
    <el-footer>
      <div class="footer">
        ©&nbsp;2019&nbsp;-&nbsp;常德人民检察院工资查询系统&nbsp;&nbsp;|&nbsp;&nbsp;常德人民检察院版权
      </div>
    </el-footer>
  </el-container>
</template>

<script>
  import {login} from '../axios/api'

  export default {
    name: "login",
    data() {
      return {
        loading: false,
        checked: false,
        loginForm: {
          username: '',
          password: ''
        }
      }
    },
    mounted: function () {
      if (this.isStorage("user")) {
        this.checked = true;
        let userMsg = JSON.parse(localStorage.getItem("user"))
        this.loginForm.username = userMsg.username
        this.loginForm.password = userMsg.password
      }
    },
    methods: {
      handleLogin:async function(){
        if (this.loginForm.username == "" || this.loginForm.password == "") {
          this.alert('warning', "请将账号密码填写完整！");
          return;
        }
        let res = await login(this.loginForm);
        localStorage.setItem("token","Bearer "+res.data.access_token);
        localStorage.setItem("userName",this.loginForm.username);
        if(res.data.is_admin){
            this.$router.push({ path:'/admin'})
        }else{
            this.$router.push({ path:'/user'})
        }
      }
    }
  }
</script>

<style>
  #app,html,body{
    margin: 0;
    padding: 0px;
  }
  .header-login {
    margin-top: 30px;
    margin-bottom: 10px;
  }

  .content {
    margin-top: 30px;
    width: 100%;
    background: url(../assets/bg.png) no-repeat center top;
    background-size: cover;
    height: 450px;
  }

  .login {
    height: 350px;
    border-radius: 6px;
    background-color: #fff;
    opacity: 0.8;
    margin-top: 30px;
    float: right;
    margin-right: 20%;
    padding: 20px;
    text-align: center;
    box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
  }

  .footer {
    text-align: center;
    width: 100%;
    margin-top: 50px;
    color: #82848a;
    font-size: 14px;
  }
</style>
